InjectHelper.addInjectCss("injects/main.css")
InjectHelper.addInjectHtmlString(document.body, '<canvas id="comment-item-render" style="display: none"/>')
InjectHelper.addInjectHtmlString(document.body, '<div onclick="downloadComments()" id="download-comment" class="download-comment-btn">下载评论</div>')
InjectHelper.addInjectScript("lib/jquery.min.js")
InjectHelper.addInjectScript("lib/GoogleTranslate.js")
InjectHelper.addInjectScript("lib/html2canvas.min.js")
InjectHelper.addInjectScript("lib/FileSaver.min.js")
InjectHelper.addInjectScript("lib/jszip.min.js")
InjectHelper.addInjectScript("injects/main.js")

insertChildAtIndex = function(parent, child, index) {
    if (!index) index = 0
    if (index >= parent.children.length) {
        parent.appendChild(child)
    } else {
        parent.insertBefore(child, parent.children[index])
    }
}

// html标记添加事件
function nodeInsertedCallback(event) {
    if (event && event.target) {
        // 评论项
        if (event.target.id === 'paid-comment-images') {
            const div = document.createElement('div');
            div.className = 'comment-select-orerate'
            div.innerHTML = '<input type="checkbox" onclick="commentItemClick(this)" name="comment-select" class="comment-select"/>';
            insertChildAtIndex(event.target.parentNode, div, 0)
        }
        // 评论内容
        else if (event.target.tagName === 'SPAN' && event.target.parentNode.id === 'content-text') {
            const text = event.target.parentNode.textContent + ''
            event.target.parentNode.setAttribute('title', text);
        }
    }
};

function nodeSubtreeUpdate(event) {
    if (event.target.id === 'content-text') {
        const lang = document.documentElement.getAttribute('lang')
        if (lang === 'zh-CN') {
            event.target.setAttribute('cn-text', event.target.textContent);
        }
    }

}
// html标记内容变化事件
document.addEventListener('DOMNodeInserted', nodeInsertedCallback);
document.addEventListener('DOMSubtreeModified', nodeSubtreeUpdate);
